//
//  JYUrlData.m
//  s.
//
//  Created by fanshijian on 2022/8/23.
//  Copyright © 2022 J. All rights reserved.
//

#import "JYUrlData.h"

NSDictionary *JYURLInfo(NSString *key) {
    //环境
    JYUrlData *datacenter = [JYUrlData shareInstance];
    if (datacenter.urls.count == 0) {
//        NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"urls" ofType:@"bundle"];
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist"]];
        if (dic) {[datacenter.urls addObject:dic];}
    }
    for (NSDictionary *en in datacenter.urls) {
        NSDictionary *urldic = en[@"REQUESTS"];
        NSMutableDictionary *urlpath = [urldic[key] mutableCopy];
        urlpath[@"host"] = en[datacenter.environmentString];
        return urlpath;
    }
    return nil;
}
NSString *JYURLString(NSString *key) {
    NSDictionary *urlpath = JYURLInfo(key);
    if (urlpath) {
        NSString *host = urlpath[@"host"];
        NSString *url  = urlpath[@"path"];
        if (url) {
            return [host stringByAppendingString:url];
        }
    }
    
    if ([NSThread isMainThread]) {
        JYLog(@"未找到对应 %@ 的url，请配置请求列表",key);
    }else{
        dispatch_async(main_queue, ^{
            JYLog(@"未找到对应 %@ 的url，请配置请求列表",key);
        });
    }
    return nil;
}

NSString *USocketURLString() {
    JYUrlData *datacenter = [JYUrlData shareInstance];
    if (datacenter.urls.count == 0) {
        NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"bundle"];
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle bundleWithPath:resourceBundle] pathForResource:@"url" ofType:@"plist"]];
        if (dic) {[datacenter.urls addObject:dic];}
    }
    for (NSDictionary *en in datacenter.urls) {
        NSDictionary *urldic = en[@"Socket"];
        return urldic[@"url"];
    }
    return nil;
}

NSDictionary *HLHTMLURLInfo(NSString *key){
    //环境
    JYUrlData *datacenter = [JYUrlData shareInstance];
    if (datacenter.urls.count == 0) {
        NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"bundle"];
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle bundleWithPath:resourceBundle] pathForResource:@"url" ofType:@"plist"]];
        if (dic) {[datacenter.urls addObject:dic];}
    }
    for (NSDictionary *en in datacenter.urls) {
        NSDictionary *urldic = en[@"HTMLS"];
        NSMutableDictionary *urlpath = [urldic[key] mutableCopy];
        urlpath[@"host"] = en[datacenter.environmentString];
        return urlpath;
    }
    return nil;
}
NSString *JYHTMLURLString(NSString *key) {
    NSDictionary *urlpath = HLHTMLURLInfo(key);
    if (urlpath) {
        NSString *host = urlpath[@"host"];
        NSString *url  = urlpath[@"path"];
        if (url) {
            return [host stringByAppendingString:url];
        }
    }
    
    if ([NSThread isMainThread]) {
        JYLog(@"未找到对应 %@ 的url，请配置请求列表",key);
    }else{
        dispatch_async(main_queue, ^{
            JYLog(@"未找到对应 %@ 的url，请配置请求列表",key);
        });
    }
    return nil;
}
@implementation JYUrlData

+ (instancetype)shareInstance {
    static JYUrlData *data = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [[JYUrlData alloc] init];
    });
    return data;
}

#pragma mark - 配置环境变量
- (void)urlAppendlist:(NSString *)path{
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    [self.urls addObject:dic];
}

- (NSMutableArray*)urls{
    NSMutableArray *_urls = objc_getAssociatedObject(self, "urls");
    if (!_urls) {
        _urls = [NSMutableArray arrayWithCapacity:2];
        objc_setAssociatedObject(self, "urls", _urls, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _urls;
}

- (environment_mode)debugModel{
#ifdef  DEBUG
    return environment_debug;
#else
    return environment_release;
#endif

}

- (NSString *)environmentString {
    environment_mode debugModel = [self debugModel];
    if (debugModel == environment_release) {
        return @"release";
    }
  
    if (debugModel == environment_debug){
        return @"debug";
    }else {
        return @"未知";
    }
}

- (NSString *)environmentName {
    environment_mode debugModel = [self debugModel];
    if (debugModel == environment_release) {
        return @"主";
    }else if (debugModel == environment_debug){
        return @"测";
    }else {
        return @"未知";
    }
}

@end
